package com.itqf.controller;

import com.itqf.annotation.RequiresPermissionsDesc;
import com.itqf.bean.DtsAdmin;
import com.itqf.bean.DtsGoods;
import com.itqf.dto.CommonsDto;
import com.itqf.service.DtsGoodsService;
import com.itqf.util.ResponseUtil;
import com.itqf.utils.AuthSupport;
import com.itqf.vo.GoodsAllinone;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * projectName: gp_manage
 *
 * @author: 赵伟风
 * time: 2021/8/23 17:06
 * description:
 */

@RestController
@CrossOrigin("*")
@RequestMapping("admin/goods/")
public class AdminDtsGoodsController {

    @Autowired
    private DtsGoodsService dtsGoodsService;

    /**
     * 商品列表接口
     *     接口:
     *     goods/list?page limit order sort goodsSn 编号 name 商品名
     *     业务分析:
     *           1.判断当前账号是不是超级管理员
     *           2.是 查询全部商品 [分页/条件]
     *           3.否 查询当前用户对应的品牌
     *           4.根据品牌找到对应的商品 [分页/条件]
     *
     * @param dto
     * @return
     */
    @RequiresPermissions("admin:goods:list")
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button="查询")
    @GetMapping("list")
    public Object list(CommonsDto dto){

        DtsAdmin dtsAdmin = AuthSupport.currentUser();

        Object object = dtsGoodsService.findGoodsByPage(dto,dtsAdmin);

        return ResponseUtil.ok(object);
    }



    @RequiresPermissions("admin:goods:detail")
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button="商品详情")
    @GetMapping("detail")
    public Object detail(Integer id) {


        Object data = dtsGoodsService.findGoodsDetailByPrimaryKey(id);

        return ResponseUtil.ok(data);
    }



    /**
     * 根据当前用户查询对应的分类和品牌列表数据
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/catAndBrand")
    public Object catAndBrand() {
        return ResponseUtil.ok(dtsGoodsService.findCatAndBrand(AuthSupport.currentUser()));
    }



    /**
     * 编辑商品
     *
     * @param goodsAllinone
     * @return
     */
    @RequiresPermissions("admin:goods:update")
    @RequiresPermissionsDesc(menu = { "商品管理", "商品管理" }, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone) {
        // logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 商品管理->商品管理->编辑,请求参数:{}", JSONObject.toJSONString(goodsAllinone));

        return dtsGoodsService.updateGoodsById(goodsAllinone);
    }

    /**
     * 删除商品
     *
     * @param goods
     * @return
     */
    @RequiresPermissions("admin:goods:delete")
    @RequiresPermissionsDesc(menu = { "商品管理", "商品管理" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGoods goods) {
        //logger.info("【请求开始】操作人:[" + AuthSupport.userName()+ "] 商品管理->商品管理->删除,请求参数:{}", JSONObject.toJSONString(goods));

        return dtsGoodsService.deleteGoodsById(goods);
    }




}
